<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${idaoPackage}.${className}DAO">

    <resultMap type="${doPackage}.${className}DO"
               id="${className}Result">
    <#list columnList as column>
        <result property="${column.filedName}" column="${column.columnName}"/>
    </#list>
    </resultMap>

    <sql id="select-param-column">
    <#list columnList as column>
        `${column.columnName}` as "${column.filedName}"<#if column_has_next>,</#if>
    </#list>
    </sql>

    <sql id="batchSet">
    <#list columnList as column>
        <if test="item.${column.filedName}!=null ">
            `${column.columnName}` = ${r"#{ item."}${column.filedName} },
        </if>
    </#list>
    </sql>


    <sql id="set">
    <#list columnList as column>
        <if test="${column.filedName}!=null ">
        `${column.columnName}` = ${r"#{ "}${column.filedName} },
        </if>
    </#list>
    </sql>

    <sql id="query-condition">
    <#list columnList as column>
<#--        <#if column.isQuery == "1">-->
            <if test="${column.filedName}!=null
             <#if column.filedType == "String">
             and ${column.filedName} != ''
             </#if>
             <#if column.filedType == "List">
             and ${column.filedName}.size>0
             </#if>
             ">
                AND `${column.columnName}` = ${r"#{"} ${column.filedName} }
            </if>
<#--        </#if>-->
    </#list>
        <if test="startTime != null">
            AND gmt_create &gt;= ${r"#{ startTime }"}
        </if>
        <if test="endTime != null">
            AND gmt_create &lt;= ${r"#{ endTime }"}
        </if>
    </sql>

    <sql id="orderby-page-limit">
        <if test="orderBy != null and orderBy != ''">
            order by ${r"${"} orderBy }
        </if>
        <if test="offset!=null and limit!=null">
            limit ${r"#{"} offset },${r"#{"} limit }
        </if>
        <if test="limit!=null and offset==null">
            limit ${r"#{"} limit }
        </if>
    </sql>

    <insert id="add" parameterType="${doPackage}.${className}DO">
        INSERT ${tableName}
        <set>
            <include refid="set"/>
        </set>
    </insert>

    <insert id="batchAdd">
        <foreach collection="list" item="item" index="index" separator=";">
            INSERT ${tableName}
            <set>
                <include refid="batchSet" />
            </set>
        </foreach>
    </insert>

    <select id="getById" resultType="${doPackage}.${className}DO">
        SELECT
        <include refid="select-param-column"/>
        from ${tableName}
        where id=${r"#{"} id }
    </select>

    <select id="getByIds" resultType="${doPackage}.${className}DO" parameterType="list">
        SELECT
        <include refid="select-param-column"/>
        from ${tableName}
        where id IN
        <foreach collection="list" item="item" open="(" separator="," close=")">
        ${r"#{"} item }
        </foreach>
    </select>

    <update id="update" parameterType="${doPackage}.${className}DO">
        update ${tableName}
        <set>
            <include refid="set"/>
        </set>
        where id=${r"#{"} id }
    </update>

    <select id="query" resultMap="${className}Result" parameterType="${queryPackage}.${className}Query">
        select
        <include refid="select-param-column"/>
        from ${tableName}
        <where>
            <include refid="query-condition"/>
        </where>
        <include refid="orderby-page-limit"/>
    </select>


    <select id="countList" resultType="int" parameterType="${queryPackage}.${className}Query">
        select
        count(1)
        from ${tableName}
        <where>
            <include refid="query-condition"/>
        </where>
    </select>

</mapper>